package net.xdclass.service.stress.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import net.xdclass.dto.stress.StressCaseDTO;
import net.xdclass.mapper.StressCaseMapper;
import net.xdclass.model.StressCaseDO;
import net.xdclass.req.stress.StressCaseSaveReq;
import net.xdclass.req.stress.StressCaseUpdateReq;
import net.xdclass.service.stress.StressCaseService;
import net.xdclass.util.SpringBeanUtil;
import org.springframework.stereotype.Service;

@Service
public class StressCaseServiceImpl implements StressCaseService {
    @Resource
    private StressCaseMapper stressCaseMapper;

    @Override
    public StressCaseDTO findById(Long projectId, Long caseId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, caseId);
        StressCaseDO stressCaseDO = stressCaseMapper.selectOne(queryWrapper);
        return SpringBeanUtil.copyProperties(stressCaseDO, StressCaseDTO.class);
    }

    @Override
    public int delete(Long id, Long projectId) {
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, projectId);
        queryWrapper.eq(StressCaseDO::getId, id);
        return stressCaseMapper.delete(queryWrapper);
    }

    @Override
    public int save(StressCaseSaveReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        return stressCaseMapper.insert(stressCaseDO);
    }

    @Override
    public int update(StressCaseUpdateReq req) {
        StressCaseDO stressCaseDO = SpringBeanUtil.copyProperties(req, StressCaseDO.class);
        LambdaQueryWrapper<StressCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StressCaseDO::getProjectId, req.getProjectId());
        queryWrapper.eq(StressCaseDO::getId, req.getId());
        return stressCaseMapper.update(stressCaseDO, queryWrapper);
    }
    //TODO:
    @Override
    public int execute(Long projectId, Long caseId) {
        return 0;
    }
}
